<#import "/spring.ftl" as spring/>
<#import "../html-macro.ftl" as h />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>添加供应商</title>
    <link rel="stylesheet" type="text/css" href="<@spring.url "/semantic/semantic.min.css" />">
<#--<script type="text/javascript" src="<@spring.url "/jquery-3.1.1.min.js" />"></script>-->
    <script type="text/javascript"
            src="<@spring.url "http://static.bgext.com/jquery-3.1.1.min.js" />"></script>
    <script type="text/javascript" src="<@spring.url "/semantic/semantic.mm.js" />"></script>
    <script type="text/javascript" src="<@spring.url "/semantic/form.min.js" />"></script>
    <script type="text/javascript" src="<@spring.url "/js/form/jquery.form.min.js"/>"></script>
</head>
<body>
<div class="main ui container">
    <div class="html ui top attached segment">
        <form class="ui form" id="cqp_form">
            <div class="field">
                <label>单位名称</label>
                <input type="text" name="companyName" placeholder="单位名称" value="">
            </div>
            <div class="field">
                <label>单位编号</label>
                <input type="text" name="companyCode" placeholder="单位编号" value=""
                       onkeyup="this.value=this.value.replace(/[\W_]/g,'')">
            </div>
            <div class="field">
                <label>单位代码（18位社会统一代码）</label>
                <input type="text" name="companyCoUnite" placeholder="单位代码（18位社会统一代码）" value="">
            </div>
            <div class="field">
                <label>单位地址</label>
                <input type="text" name="companyAddress" placeholder="单位地址" value="">
            </div>
            <div class="field">
                <label>单位联系电话</label>
                <input type="text" name="companyPhone" placeholder="单位联系电话" value="">
            </div>
            <div class="field">
                <label>单位传真</label>
                <input type="text" name="companyFax" placeholder="单位传真" value="">
            </div>
            <div class="ui grid centered">
                <div class="row">
                    <button class="ui primary button cqp_submit" type="button">提交</button>
                    <button class="ui button" type="reset">清空</button>
                </div>
            </div>
            <div class="ui error message"></div>
        </form>
        <div class="ui error message hidden" id="sys_err">
        </div>
        <div class="ui positive message hidden">
            <p><b>信息已录入系统，请耐心等待审核结果...</b></p>

            <p>
                <a href="javascript:void(0);" id="continueAddCqp"><b>继续录入</b></a>
                或者
                <a href="javascript:void(0);" id="closeFrame" style="color:#8F9BA7;"><b>关闭窗口</b></a>
            </p>
        </div>
    </div>
</div>
<script type="text/javascript">
    function validation() {
        var form = document.forms.cqp_form;
        $(form).form({
            fields: {
                companyName: {
                    identifier: 'companyName',
                    rules: [
                        {
                            type: 'empty',
                            prompt: '请输入单位名称'
                        }
                    ]
                },
                companyCode: {
                    identifier: 'companyCode',
                    rules: [
                        {
                            type: 'empty',
                            prompt: '请输入单位编号'
                        }, {
                            type: 'regExp[/^[0-9a-zA-Z]*$/g]',
                            prompt: '单位编号只能录入数字和英文'
                        }
                    ]
                },
                companyCoUnite: {
                    identifier: 'companyCoUnite',
                    optional: 'true',
                    rules: [
                        {
                            type: 'maxLength[18]',
                            prompt: '18位单位代码'
                        }
                    ]
                }
            }
        });
    }

    /**
     * 显示操作成功的消息
     */
    var showSuccess = function () {
        var $positiveMessage = $("div.ui.positive.message");
//        $positiveMessage.text("提交成功");
        $positiveMessage.show();
    }

    /**
     * 提交事情绑定和逻辑
     */
    var submitBind = function () {
        //var $cqpForm = ($("#cqp_form")).replace(/\s/g,"");
        var $cqpForm = $("#cqp_form");
        var $submitBtn = $(".cqp_submit");
        $submitBtn.click(function () {
            var companyName = $("input[name='companyName']").val();
            $.ajax({
                url: "/business/companyQp/checkExistByCompanyName?companyName="
                + companyName,
                async: false,
                success: function (data) {
                    if (data) {
                        alert("该单位名称[" + companyName + "]已提交审核，不能重复添加");
                    } else {
                        $cqpForm.submit();
                    }
                }
            });
        });
        /* function trim(str){
             return str.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'');
         }*/
        $cqpForm.submit(function () {
            trim($cqpForm);
            $(this).ajaxSubmit({
                type: 'post', // 提交方式 get/post
                dataType: 'json',
                //data: $cqpForm.val().replace(' ',''),
                //data: $.trim($cqpForm.val()),
                url: "<@spring.url '/business/companyQp/j_cqpt_save'/>",
                beforeSubmit: function () {
                    var valid = $cqpForm.form("is valid");
                    if (valid) {
                        $cqpForm.addClass("loading");
                    }
                    return valid;
                },
                success: function (data) {
                    if (data.rs) {
//                                           showSuccess();
//                                           $("input").val("");
//                                           $("button").addClass("disabled");
                        sessionStorage.clear();
                        parent.layer.closeAll();
                    } else {
                        $("#sys_err").text("出问题了，请联系客服").show();
                    }
                },
                complete: function () {
                    $cqpForm.removeClass("loading");
                }
            });
            return false;
        });
    }

    /**
     * 去除整个表单数据的空格
     */
    var trim = function ($form) {
        //表单验证
        var $inputs = $form.find("input");
        $.each($inputs, function (i, n) {
            var $input = $(n);
            $input.val($input.val().replace(/ /g,''));
            //alert($input.val().replace(/[\W_]/g,''));
        });
    };
    /**
     * 成功提交后的按钮事件
     */
    var successBtnsBind = function () {
        /**
         * 继续录入
         */
        var continueAddCompanyQp = function () {
            location.reload();
        }

        /**
         * 关闭窗口
         */
        var closeFrame = function () {
            layer.close();
        }
        $("a#continueAddCqp").click(continueAddCompanyQp);
        $("a#closeFrame").click(closeFrame);
    }

    $(function () {
        validation();
        submitBind();
        successBtnsBind();
    });
</script>
</body>
</html>
